//
//  VKRefriendsController.m
//  V-Learn
//
//  Created by liubo on 2019/1/10.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKRefriendsController.h"
#import "VKReManagerController.h"
#import "QiCodeManager.h"
//#import "WXWebpageObject.h"
@interface VKRefriendsController ()
@property (nonatomic, strong) UIImageView *bgImageV;
@property (nonatomic, strong) UIImageView *qcImageView;
@end

@implementation VKRefriendsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"推荐好友";
    [self setupRightItem];
    [self setupUI];
    [self requestImagesData];
    //TuijianFriendWeChatShareSuccess
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(senWeChatShareSuccess) name:@"TuijianFriendWeChatShareSuccess" object:nil];
    
}
#pragma mark - 分享成功后调用积分接口
- (void)senWeChatShareSuccess{
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    NSString *token = [UserModel getUserModel].token;
    [data setObject:token forKey:@"token"];
    [WYHXNetWorkManager post:WKQshareRecommendData_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        NSString *msg = responseObj[@"msg"];
        if ([code integerValue]==200) {
//            NSLog(@"%@",responseObj);
            NSString *data = responseObj[@"data"];
            [MBProgressHUD showSuccess:msg toView:self.view];
            return ;
        }
    }];
}
#pragma mark - 获取图片接口
- (void)requestImagesData{
    //    ImageConfigController/queryBackImage
    __weak typeof(self)weakSelf = self;
    [WYHXNetWorkManager get:WKQueryBackImage_WKURL params:@{} completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
//            NSLog(@"%@",responseObj);
            NSDictionary *dic = responseObj[@"data"];
            //            NSString *clockUpImage = dic[@"clockUpImage"];
            //            NSString *clockDownImage = dic[@"clockDownImage"];
            NSString *recommendImage = dic[@"recommendImage"];
            
            NSString *downurlString = [NSString stringWithFormat:@"%@%@",Public_URL,recommendImage];
            [self.bgImageV sd_setImageWithURL:[NSURL URLWithString:downurlString] placeholderImage:[UIImage imageNamed:@"bg111"]];
        }
        __block CGSize size = self.qcImageView.bounds.size;
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *code = [UserModel getUserModel].userId;//self.userId;
            UIImage *codeImage = [QiCodeManager generateQRCode:code size:size];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.qcImageView.image = codeImage;
            });
        });
    }];
    
}
- (void)setupUI{
    UIImageView *bgImageV = [[UIImageView alloc]init];
    self.bgImageV = bgImageV;
    [self.view addSubview:bgImageV];
    bgImageV.image = [UIImage imageNamed:@"bg111"];
    [bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    //    UIImageView *bottomImageV = [[UIImageView alloc]init];
    //    [self.view addSubview:bottomImageV];
    //    bottomImageV.image = [UIImage imageNamed:@"bg333"];
    //    [bottomImageV mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.right.bottom.equalTo(@0);
    //        make.height.equalTo(@200);
    //    }];
    //
    //    UIImageView *topImageV = [[UIImageView alloc]init];
    //    [self.view addSubview:topImageV];
    //    topImageV.image = [UIImage imageNamed:@"bg222"];
    //    [topImageV mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(@60);
    //        make.height.equalTo(@300);
    //        make.width.equalTo(@260);
    //        make.centerX.equalTo(self.view);
    //    }];
    
    
    
    UIImageView *qcImageView = [[UIImageView alloc] init];
    qcImageView.image = [UIImage imageNamed:@""];
    self.qcImageView = qcImageView;
    [bgImageV addSubview:qcImageView];
    //    qcImageView.frame = CGRectMake((Left-10)*kScaleFit, 85*kScaleFit, 170*kScaleFit, 170*kScaleFit);
    [qcImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(110);
        make.size.mas_equalTo(CGSizeMake(250, 250));
                make.centerX.mas_equalTo(bgImageV);
//        make.left.mas_equalTo(350);
    }];
//    __block CGSize size = qcImageView.bounds.size;
//
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        NSString *code = [UserModel getUserModel].userId;//self.userId;
//        UIImage *codeImage = [QiCodeManager generateQRCode:code size:size];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            qcImageView.image = codeImage;
//        });
//    });
    UIButton *shareBtn = [[UIButton alloc]init];
    [self.view addSubview:shareBtn];
    [shareBtn setImage:[UIImage imageNamed:@"btn-share"] forState:UIControlStateNormal];
    [shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(qcImageView.mas_bottom).offset(30);
        make.height.equalTo(@55);
        make.width.equalTo(@160);
        make.centerX.equalTo(self.view);
    }];
    [shareBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *shareLabel = [[UILabel alloc]init];
    [self.view addSubview:shareLabel];
    shareLabel.text = @"分享";
    shareLabel.textColor = [UIColor whiteColor];
    shareLabel.font = [UIFont systemFontOfSize:17];
    [shareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(shareBtn).offset(13);
        make.centerY.mas_equalTo(shareBtn);
        make.centerX.equalTo(shareBtn);
    }];
}
- (void)shareBtnClick{
    [LWShareService shared].shareBtnClickBlock = ^(NSIndexPath *indexPath) {
        NSInteger tag = indexPath.row;
        [[LWShareService shared] hideSheetView];
//        NSLog(@"tag:%ld",tag);
        //https://www.vlearnjy.com/html/detail/share.html?userId=130@17661044588@1556258005655&username=%E6%9D%8E%E5%B2%A9%E6%B5%8B%E8%AF%95
//https://www.vlearnjy.com/html/detail/share.html?userId=168@17634274326@1556261758523&username=168
//        NSString *shareUrl = [NSString stringWithFormat:@"http://baidu.com"];

#warning TODO 给后台确认一下，传那个参数 是昵称 还是userid
        NSString *shareUrl = [NSString stringWithFormat:@"%@%@&username=%@",WKRecommendFriend_url,[UserModel getUserModel].token,self.userName];
        switch (tag) {
            case 0:
            {
                //http://47.95.9.40/html/detail/register.html?userId=147@15922048322@1554969609046

                WXWebpageObject *webpageObject = [WXWebpageObject object];
                webpageObject.webpageUrl = shareUrl;//@"http://wanke.value19.com/html/detail/course_detail.html?courseId=53";
                
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = @"推荐好友";//self.model.courseTitle;
                message.description = @"";//self.model.courseInfo;
//                [message setThumbImage:[UIImage imageNamed:self.qcImageView]];
                [message setThumbImage:[UIImage imageNamed:@"wanke_logo"]];
                message.mediaObject = webpageObject;
                
                SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneSession;
                [WXApi sendReq:req];
            }
                break;
            case 1:
            {

                WXWebpageObject *webpageObject = [WXWebpageObject object];
                webpageObject.webpageUrl = shareUrl;
                
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = @"推荐好友";;//self.model.courseTitle;
                message.description = @"";//self.model.courseInfo;
//                [message setThumbImage:[UIImage imageNamed:self.model.courseImage]];
                [message setThumbImage:[UIImage imageNamed:@"wanke_logo"]];
                message.mediaObject = webpageObject;
                
                SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneTimeline;
                [WXApi sendReq:req];
                
            }
                break;
            default:
                break;
        }
    };
    
    [[LWShareService shared] showInViewController:self];
//            [[LWShareService shared] showInViewController:self];
}
// 右边Item
- (void)setupRightItem{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"管理" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor cz_colorWithRed:40 green:40 blue:40] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 40, 30);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button] ;
    //    [button mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.height.equalTo(@30);
    //        make.width.equalTo(@40);
    //    }];
    [button addTarget:self action:@selector(buttonItemClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonItemClick{
    [self presentViewController:[[VKReManagerController alloc]init] animated:YES completion:nil];
}

@end
